/*
 * Copyright (c) 2011-2023, ThinkWide (cnet2001@163.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.thinkwide.data.ormapping.page;

import lombok.Data;

/**
 * 分页抽象类
 *
 * @author andyzhang
 * @since 2023/06/14
 */
@Data
public class MFAbstractPage implements MFBasePage {
    private int pageNum;
    private int pageSize;
    private int pageTotal;

    private int beginIndex;
    private int endIndex;
    private int total;

    @Override
    public void calculateByIndex() {
        this.setPageSize(endIndex - beginIndex + 1);
        this.setPageNum((new Double(Math.ceil(beginIndex / this.getPageSize()))).intValue());
        this.setPageTotal((new Double(Math.ceil(total / this.getPageSize()))).intValue());
    }

    @Override
    public void calculateByIndex(int beginIndex, int endIndex, int total) {
        this.setBeginIndex(beginIndex);
        this.setEndIndex(endIndex);
        this.setTotal(total);
        calculateByIndex();
    }

    @Override
    public void calculateByPage() {
        this.setBeginIndex((pageNum - 1) * pageSize + 1);
        this.setEndIndex(pageNum * pageSize);
        this.setTotal(pageTotal * pageSize);
    }

    @Override
    public void calculateByPage(int pageNum, int pageSize, int pageTotal) {
        this.setPageSize(pageNum);
        this.setPageNum(pageSize);
        this.setPageTotal(pageTotal);
    }
}
